package kr.co.tomatosystem.pdetools.shell.internal.win32;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import kr.co.tomatosystem.pdetools.shell.internal.AbstractShowInShellHandler;
import kr.co.tomatosystem.pdetools.shell.internal.util.ProcessKiller;

import org.eclipse.ui.commands.IElementUpdater;
import org.eclipse.ui.menus.UIElement;

public class ShowInShellHandlerWIN32 extends AbstractShowInShellHandler implements IElementUpdater {
	@Override
	protected void doReveal(final File file) {
		try {
			ProcessBuilder builder = null;
			if (file.isDirectory()) {
				builder = new ProcessBuilder("explorer", String.format("\"%s\"", file.getAbsolutePath())); //$NON-NLS-1$ //$NON-NLS-2$ 
			} else {
				builder = new ProcessBuilder("explorer", "/select,", String.format("\"%s\"", file.getAbsolutePath())); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			}
			Process process = builder.start();

			// 무슨 이유던 이 프로세스가 제대로 종료되지 않는 경우 5초뒤 강제 종료.
			new ProcessKiller(process).schedule(5000);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void updateElement(UIElement element, Map parameters) {
		element.setText(Messages.ShowInShellHandlerWIN32_SHOW_IN_EXPLORER);
	}
}
